import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';

import zipPack from 'vite-plugin-zip-pack';

import { resolve } from 'path';
const pathSrc = resolve(__dirname, 'src');
// https://vite.dev/config/
export default defineConfig({
  base: './',
  resolve: {
    alias: {
      '@': pathSrc
    }
  },
  server: {
    host: '0.0.0.0', // 服务器主机名，如果允许外部访问，可设置为"0.0.0.0"
    port: 4567,
    open: false,
    cors: true,
    proxy: {
      '/api': {
        target: 'http://10.60.13.140:1081', // easymock
        changeOrigin: true,
        ws: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  css: {
    // CSS 预处理器
    preprocessorOptions: {
      //define global scss variable
      scss: {
        javascriptEnabled: true,
        additionalData: `@use "@/styles/variables.scss" as *;`
      }
    }
  },
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ['vue', '@vueuse/core', 'vue-router'],
      eslintrc: {
        enabled: false,
        filepath: './.eslintrc-auto-import.json',
        globalsPropValue: true
      },
      vueTemplate: true,
      dts: resolve(pathSrc, 'types', 'auto-imports.d.ts'),
      resolvers: [AntDesignVueResolver({ importStyle: 'css', resolveIcons: true })]
    }),
    Components({
      dirs: [resolve(pathSrc,'components'),resolve(pathSrc,'views')],
      dts: resolve(pathSrc, 'types', 'components.d.ts'),
      resolvers: [AntDesignVueResolver({ importStyle: 'css', resolveIcons: true })]
    }),
    zipPack({
      outFileName: 'dist.zip',
      outDir: 'dist'
    })
  ],
  build: {
    chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
    minify: 'terser', // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
    terserOptions: {
      compress: {
        keep_infinity: true, // 防止 Infinity 被压缩成 1/0，这可能会导致 Chrome 上的性能问题
        drop_console: true, // 生产环境去除 console
        drop_debugger: true // 生产环境去除 debugger
      },
      format: {
        comments: false // 删除注释
      }
    }
  }
});
